﻿/// <reference path="DataService.js" />
/// <reference path="../Plugins/date.format.js" />
/// <reference path="../HtmlDesign/lib/js/jquery-1.10.2.min.js" />

//LibDocument class
function SearchDocuments(total, pageSize, pageIndex, search) {

    //Private fileds
    var _pageSize = pageSize;
    var _pageIndex = pageIndex;
    var _total = total;
    var _search = search;
    var _baseUrl = '/api/ApiLibDocuments/SearchBy1Condition';

    var _dataService = new DataService();

    //as
    this.Init = function () {
        $('#mainSearch').val(search);
        Paging();

        //right menu: Get category
        $('#rmCategories div.list-group').empty();
        _dataService.Get('/api/ApiLibCategories/GetAll', function (data, textStatus, jqXHR) {
            if (data.success) {
                if (data.total <= 0) {
                    $('#rmCategories div.list-group').append('<a href="#" class="list-group-item">Chưa có danh mục nào trong hệ thống</a>');
                }
                else {
                    for (var i = 0; i < data.data.length; i++) {
                        var html = '<a href="/documents/category/' + data.data[i].CategoryId + '" class="list-group-item">' + (i + 1) + '. ' + data.data[i].CategoryName + '</a>';
                        $('#rmCategories div.list-group').append(html);
                    }
                }
            }
            else {
                $('#rmCategories').remove();
            }
        });

        //right menu: Get top 5 Documents by viewcount
        $('#rmDocumentsTopView div.list-group').empty();
        _dataService.Get('/api/ApiLibDocuments/GetTop5View', function (data, textStatus, jqXHR) {
            if (data.success) {
                if (data.data.length <= 0) {
                    $('#rmDocumentsTopView div.list-group').append('<a href="#" class="list-group-item">Chưa có tài liệu nào trong hệ thống</a>');
                }
                else {
                    for (var i = 0; i < data.data.length; i++) {
                        var html = '<a href="/documents/detail/' + data.data[i].DocumentId + '" class="list-group-item">' +
                                        '<div class="media">' +
                                            '<img class="media-object pull-left" src="' + ((data.data[i].ThumbnaiPath != null) ? '../../Contents/Image/DocumentThumbnails/' + data.data[i].ThumbnaiPath : '../../Contents/Image/thumbnail-none.gif') + '" style="width: 40px; height: 50px;" alt="...">' +
                                            '<div class="media-body">' +
                                                '<p>' + data.data[i].DocumentName + '</p>' +
                                                '<p class="text-success">Lượt xem: <span class="badge">' + data.data[i].ViewCount + '</span></p>' +
                                            '</div>' +
                                        '</div>' +
                                    '</a>';

                        $('#rmDocumentsTopView div.list-group').append(html);
                    }
                }
            }
            else {
                $('#rmDocumentsTopView').remove();
            }
        });
    };

    //private function
    function Paging() {
        var totalPages = (_total < pageSize) ? 1 : Math.floor(_total / _pageSize) + 1;
        if (_total > 0 && _total > pageSize) {
            var options = {
                bootstrapMajorVersion: 3,
                currentPage: _pageIndex,
                totalPages: totalPages,
                size: 'normal',
                useBootstrapTooltip: true,
                tooltipTitles: function (type, page, current) {
                    switch (type) {
                        case "first":
                            return "Về trang đầu";
                        case "prev":
                            return "Trang trước";
                        case "next":
                            return "Trang tiếp";
                        case "last":
                            return "Về trang cuối";
                        case "page":
                            return "Trang " + page;
                    }
                },
                onPageClicked: function (e, originalEvent, type, page) {
                    var url = _baseUrl + '?index=' + page + '&size=' + _pageSize + '&search=' + _search;

                    _dataService.Get(url, successCallback);
                }
            };
            $('#paging').bootstrapPaginator(options);
        }
    }

    function successCallback(data, textStatus, jqXHR) {
        InsertDocumentItem(data);
        if (data.total != _total) {
            var newTotal = (data.total < pageSize) ? 1 : Math.floor(data.total / _pageSize) + 1;
            var newOptions = {
                totalPages: newTotal
            };
            $('#paging').bootstrapPaginator(newOptions);
        }
    }

    function InsertDocumentItem(data) {
        if (!data) {
            return;
        }
        $('#documentItems').empty();
        for (var i = 0; i < data.data.length; i++) {
            var html = '<div class="panel panel-default">' +
                            '<div class="panel-body">' +
                                '<div>' +
                                    '<a href="/documents/detail/' + data.data[i].DocumentId + '">' +
                                        '<h3><strong>' + ((data.data[i].DocumentName != null) ? data.data[i].DocumentName : '') + '</strong></h3>' +
                                    '</a>' +
                                '</div>' +
                                '<div class="well well-sm">' +
                                    '<div class="row">' +
                                        '<div class="col-lg-8 col-md-8">' +
                                            '<b>Danh mục: </b>' +
                                            '<a class="label label-default" href="#" rel="tag">' + ((data.data[i].CategoryName != null) ? data.data[i].CategoryName : '') + '</a>' +
                                        '</div>' +
                                        '<div class="col-lg-4 col-md-4">' +
                                            '<b>Đăng ngày: </b>' +
                                            '<abbr class="published" title="' + ((data.data[i].CreateDate != null) ? new Date(data.data[i].CreateDate).format('dd/mm/yyyy HH:MM:ss') : '') + '">' + ((data.data[i].CreateDate != null) ? new Date(data.data[i].CreateDate).format('dd/mm/yyyy') : '') + '</abbr>' +
                                        '</div>' +
                                    '</div>' +
                                '</div>' +
                                '<div class="row" style="padding-left:15px;">' +
                                    '<div class="media">' +
                                        '<a class="pull-left" href="/documents/detail/' + data.data[i].DocumentId + '">' +
                                            '<img class="img-thumbnail media-object" alt="100x120" style="width: 100px; height: 120px;" src="' + ((data.data[i].ThumbnaiPath != null) ? '../../Contents/Image/DocumentThumbnails/' + data.data[i].ThumbnaiPath : '../../Contents/Image/thumbnail-none.gif') + '">' +
                                        '</a>' +
                                        '<div class="media-body">' +
                                            '<h4 class="media-heading">Mô tả tài liệu: </h4>' +
                                            ((data.data[i].Descriptions != null) ? data.data[i].Descriptions : '') +
                                        '</div>' +
                                    '</div>' +
                                '</div>' +
                                '<div class="row xuong">' +
                                    '<div class="col-md-10 col-sm-10">' +
                                        '<p class="text-success"><strong>Lượt xem: <span class="badge">' + ((data.data[i].ViewCount != null) ? data.data[i].ViewCount : 0) + '</span></strong></p>' +
                                    '</div>' +
                                    '<div class="col-md-2 col-sm-2">' +
                                        '<a class="btn btn-success" href="/documents/detail/' + data.data[i].DocumentId + '"><strong>Chi tiết ...</strong></a>' +
                                    '</div>' +
                                '</div>' +
                            '</div>' +
                        '</div>';
            $('#documentItems').append(html);
        }
    }
}